/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helpdesk.controllers.util;

import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author JK
 */
public class Cookier {

    /**
     * cookie maxAge in second default: 1 day
     */
    public static int maxAge = 60 * 60 * 24;

    public Cookier() {
    }

    public static String getValue(String name) {
        FacesContext c = FacesContext.getCurrentInstance();
        if (c != null) {
            ExternalContext ec = c.getExternalContext();
            Cookie[] cookies = ((HttpServletRequest) ec.getRequest()).getCookies();
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public static Cookie get(String name) {
        FacesContext c = FacesContext.getCurrentInstance();
        if (c != null) {
            ExternalContext ec = c.getExternalContext();
            Cookie[] cookies = ((HttpServletRequest) ec.getRequest()).getCookies();
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie;
                }
            }
        }
        return null;

    }

    public static boolean add(String name, String value) {
        FacesContext c = FacesContext.getCurrentInstance();
        if (c != null) {
            ExternalContext ec = c.getExternalContext();
            Cookie cookie = new Cookie(name, value);
            cookie.setMaxAge(maxAge);
            HttpServletResponse response = (HttpServletResponse) ec.getResponse();
            response.addCookie(cookie);
            return true;
        }
        return false;

    }

    public boolean add(String name, String value, String path) {
        FacesContext c = FacesContext.getCurrentInstance();
        if (c != null) {
            ExternalContext ec = c.getExternalContext();
            Cookie cookie = new Cookie(name, value);
            cookie.setMaxAge(maxAge);
            cookie.setPath(path);
            HttpServletResponse response = (HttpServletResponse) ec.getResponse();
            response.addCookie(cookie);
            return true;
        }
        return false;
    }

    public static boolean remove(String name) {
        FacesContext c = FacesContext.getCurrentInstance();
        if (c != null) {
            ExternalContext ec = c.getExternalContext();
            Cookie[] cookies = ((HttpServletRequest) ec.getRequest()).getCookies();
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    Cookie other = cookie;
                    other.setMaxAge(0);
                    ((HttpServletResponse) ec.getResponse()).addCookie(other);
                    return true;
                }
            }
        }
        return false;

    }

    public static boolean exist(String name) {
        FacesContext c = FacesContext.getCurrentInstance();
        if (c != null) {
            ExternalContext ec = c.getExternalContext();
            Cookie[] cookies = ((HttpServletRequest) ec.getRequest()).getCookies();
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                   return true;
                }
            }
        }
        return false;
    }
}
